package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.RolePermission;
import com.itheima.service.EmpService;
import com.itheima.service.EmproleService;
import com.itheima.service.RolePermissionService;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @PostMapping("/add")
    public Result add(@RequestBody Role role) {
        roleService.save(role);
        return Result.success("成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.success("成功");
    }

    @PutMapping("/put/{id}")
    public Result update(@PathVariable Long id, @RequestBody Role role) {
        // 校验 ID 是否匹配
        if (id == null) {
            return Result.error("请求的ID与数据中ID不一致");
        }

        // 更新操作
        boolean isUpdated = roleService.updateById(role);

        if (isUpdated) {
            return Result.success("成功");
        } else {
            return Result.error("更新失败");
        }
    }

    // 获取角色信息
    @GetMapping("/detail/{id}")
    public Result getRoleById(@PathVariable Long id) {
        // 根据 ID 查询角色信息
        Role role = roleService.getById(id);

        // 如果没有找到对应的角色信息，返回错误信息
        if (role == null) {
            return Result.error("未找到该角色信息");
        }

        // 创建 LambdaQueryWrapper 来构造查询条件
        LambdaQueryWrapper<RolePermission> rolepermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，查询该角色对应的所有权限
        rolepermLambdaQueryWrapper.eq(RolePermission::getRoleId, id);

        // 查询数据，假设是一个 List<Roleperm>
        List<RolePermission> roleperms = rolePermissionService.list(rolepermLambdaQueryWrapper);

        // 提取权限 id 存入 permIds 数组
        List<Integer> permIds = roleperms.stream()
                .map(RolePermission::getPermissionId)  // 提取权限的 id
                .collect(Collectors.toList());

        // 将权限 id 列表放入角色对象中
        JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(role));
        object.put("permIds", permIds);

        // 返回成功的响应，包含角色信息和权限 id
        return Result.success(object);
    }


    @GetMapping("/list")
    public Result<Page> page(Integer page, Integer pagesize) {
        Page page1 = new Page(page, pagesize);
        return roleService.selectPage(page1);
    }
    @GetMapping("/list/enabled")
    public Result<Page> page1(Integer page, Integer pagesize,Integer state) {
        Page page1 = new Page(page, pagesize);
        return roleService.selectPage1(page1,state);
    }

}
